package cn.cycleif.app.service;

import cn.cycleif.app.domain.dto.TagAddDTO;
import cn.cycleif.app.mapper.TagMapper;
import cn.cycleif.entity.Tag;
import cn.cycleif.enums.CommonStatusEnum;
import cn.cycleif.enums.ResultCodeEnum;
import cn.cycleif.util.PageUtil;
import cn.cycleif.util.R;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Date: 25/2/6-17:09
 * @Desc:
 */
@Service
public class TagService extends ServiceImpl<TagMapper, Tag> {

  @Autowired
  private TagMapper tagMapper;

  public R<String> addTag(TagAddDTO dto) {
    LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Tag::getSlugName, dto.getSlugName());
    List<Tag> tags = tagMapper.selectList(wrapper);
    if (tags.size() > 0)         return R.error("标签已存在");

    Tag tag = BeanUtil.copyProperties(dto, Tag.class);
    tag.setQuestionCount(0);
    tagMapper.insert(tag);
    return R.success();
  }

  public R<String> updateTag(Tag tag) {
    tagMapper.updateById(tag);
    return R.success();
  }

  public R<String> deleteTag(Integer id) {
    Tag tag = tagMapper.selectById(id);
    if (tag == null) return R.error(ResultCodeEnum.ERROR_NOT_EXIST);

    if (tag.getQuestionCount() != 0) return R.error(ResultCodeEnum.ERROR_NOT_ALLOW);

    tagMapper.deleteById(tag);
    return R.success();
  }

  public R<Tag> getTag(Integer id) {
    return R.success(tagMapper.selectById(id));
  }

  public R<Object> getPage(String order) {
    Page<Tag>               page    = new Page<>(PageUtil.getPageNo(), PageUtil.getPageSize());
    LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
    switch (order) {
      case "hot":
        wrapper.orderByDesc(Tag::getQuestionCount);
        break;
      case "alphabetical":
        wrapper.orderByAsc(Tag::getSlugName);
        break;
      case "newest":
      default:
        wrapper.orderByDesc(Tag::getCreateAt);
        break;
    }
    Page<Tag> questionPage = tagMapper.selectPage(page, wrapper);

    return R.success(questionPage);
  }

  public R<Tag> getTagByName(String id) {
    LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Tag::getSlugName, id);

    Tag tag = tagMapper.selectOne(wrapper);
    return tag == null
        ? R.error(ResultCodeEnum.ERROR_NOT_EXIST)
        : R.success(tag);
  }

  public R<Object> getListAll() {
    List<Tag> tags = tagMapper.selectList(null);
    List<HashMap<String, Object>> list =
        tags.stream().map(tag -> {
          HashMap<String, Object> map = new HashMap<>();
          map.put("id", tag.getId());
          map.put("displayName", tag.getDisplayName());
          return map;
        }).toList();
    return R.success(list);
  }
}
